package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockDebtMapper;
import cc.mcggdd.leah.boot.stock.service.StockDebtService;
import cc.mcggdd.leah.boot.stock.model.entity.StockDebt;
import cc.mcggdd.leah.boot.stock.model.form.StockDebtForm;
import cc.mcggdd.leah.boot.stock.model.query.StockDebtQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockDebtVO;
import cc.mcggdd.leah.boot.stock.converter.StockDebtConverter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 负债管理服务实现类
 *
 * @author 
 * @since 2025-02-10 11:15
 */
@Service
@RequiredArgsConstructor
public class StockDebtServiceImpl extends ServiceImpl<StockDebtMapper, StockDebt> implements StockDebtService {

    private final StockDebtConverter stockDebtConverter;

    /**
    * 获取负债管理分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockDebtVO>} 负债管理分页列表
    */
    @Override
    public IPage<StockDebtVO> getStockDebtPage(StockDebtQuery queryParams) {
        Page<StockDebtVO> pageVO = this.baseMapper.getStockDebtPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取负债管理表单数据
     *
     * @param id 负债管理ID
     * @return
     */
    @Override
    public StockDebtForm getStockDebtFormData(Long id) {
        StockDebt entity = this.getById(id);
        return stockDebtConverter.toForm(entity);
    }
    
    /**
     * 新增负债管理
     *
     * @param formData 负债管理表单对象
     * @return
     */
    @Override
    public boolean saveStockDebt(StockDebtForm formData) {
        StockDebt entity = stockDebtConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新负债管理
     *
     * @param id   负债管理ID
     * @param formData 负债管理表单对象
     * @return
     */
    @Override
    public boolean updateStockDebt(Long id,StockDebtForm formData) {
        StockDebt entity = stockDebtConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除负债管理
     *
     * @param ids 负债管理ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockDebts(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的负债管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public BigDecimal getStockDebtTotalFee() {
        return this.baseMapper.getStockDebtTotalFee();
    }

    @Override
    public List<StockDebt> getAllRecord() {
        return this.baseMapper.selectList(new QueryWrapper<StockDebt>());
    }

}
